import {Component, OnInit} from '@angular/core';
import {LoadingService} from '../../../shared/loading.service';
import {AlertService} from '../../../shared/alert.service';
import {UserService} from '../user.service';
import {ActivatedRoute, Router} from '@angular/router';

@Component({
    selector: 'app-user-list',
    templateUrl: './user-list.component.html',
    styleUrls: ['./user-list.component.scss']
})
export class UserListComponent implements OnInit {

    public IMG_URL: any = IMG_URL;
    public keys: any = '';
    public value: any = '';
    public nickname: any = '';
    public dataArr: any = [1];
    public pages: Number = 1;
    public isNodata: Boolean = false;
    public totalItems: any; // 列表数据总数
    public currentPage: any = 1; // 默认的开始页数
    public maxSize: Number = 3; // 显示的最大按钮数量

    public query_params: any = [];

    constructor(public loading: LoadingService,
                public service: UserService,
                public alert: AlertService,
                public router: Router,
                public route: ActivatedRoute) {
    }


    ngOnInit() {
        this.route.queryParams.subscribe(params => {
            console.log(params, '分页数据');
            if (params['page']) {
                this.nickname = params['nickname'];
                this.currentPage = Number(params['pageNum']);
                this.value = params['phoneNum'];
                this.pages = params['page'];
            }
        });
        this.getData();
    }

    keyChanges() {
        this.value = '';
        this.nickname = '';
    }

    // 获取列表数据
    getData() {
        this.loading.show();
        const params: any = {
            nickName: this.nickname,
            phoneNum: this.value,
            page: this.pages,
        };
        console.log(params);
        this.router.navigate([], {queryParams: this.query_params});
        this.service.getList(params).subscribe(
            res => {
                console.log(res, '数据');
                if (res['code'] === 0) {
                    this.isNodata = false;
                    if (res['data'].length === 0) {
                        this.isNodata = true;
                        this.dataArr = [];
                    } else {
                        this.dataArr = res['data'];
                        for (let i = 0; i < this.dataArr.length; i++) {
                            if (this.dataArr[i]['avatar'].substring(0, 4) === 'http') {
                                this.dataArr[i]['avatar'] = this.dataArr[i]['avatar'];
                            } else {
                                this.dataArr[i]['avatar'] = IMG_URL + this.dataArr[i]['avatar'];
                            }
                            this.dataArr[i]['add_time'] = this.formatDateTime(this.dataArr[i]['add_time'].substring(0, 10) * 1000);
                        }
                        this.totalItems = res['count'];
                    }
                    this.loading.hide();
                } else {
                    this.loading.hide();
                    this.alert.show(res['msg'])
                }
            },
            error => {
                this.loading.hide();
                this.alert.show('网络错误');
            }
        );
    }

    formatDateTime(inputTime) {
        const time = new Date(parseInt(inputTime));
        const y = time.getFullYear();
        const m = time.getMonth() + 1;
        const min = m < 10 ? ('0' + m) : m;
        const d = time.getDate();
        const day = d < 10 ? ('0' + d) : d;
        const h = time.getHours();
        const hours = h < 10 ? ('0' + h) : h;
        const minute = time.getMinutes();
        const second = time.getSeconds();
        const minutes = minute < 10 ? ('0' + minute) : minute;
        const seconds = second < 10 ? ('0' + second) : second;
        // return y + '.' + min + '.' + day;
        return y + '-' + min + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
    };

    // 分页
    pageChanged(event) {
        this.pages = event.page;
        this.router.navigate([], {queryParams: this.query_params});
        this.loading.show();
        this.getData();
        setTimeout(() => {
            this.loading.hide();
        }, 300);
    }
}
